import logging

import pytest
import requests

from api.recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.utils import common_assert, html_util, read_json


class TestRecharge(object):

    def setup(self):
        # 创建session对象
        ses = requests.session()
        # 实例化登录模块封装的类
        self.login_api = RegLoginApi(ses)
        # 实例化充值模块封装的类
        self.rech_api = RechargeApi(ses)
        # 获取充值验证码测试用例
    @pytest.mark.parametrize('r,exp',read_json('recharge_data','recharge_verify_code'))
    def test01_get_recharge_verify_code(self,r,exp):
        # 获取充值图片验证码
        resp = self.rech_api.get_recharge_verify_code(r)
        # logging.info(resp)
        # 断言
        assert exp == resp.status_code

    # 充值测试用例
    @pytest.mark.parametrize('money,code,exp_s,exp_d',read_json('recharge_data','recharge'))
    def test02_recharge(self,money,code,exp_s,exp_d):
        # 登录
        self.login_api.user_login('18928450131', 'qqq123')
        # 获取充值图片验证码
        self.rech_api.get_recharge_verify_code(0.133333)
        # 充值
        resp = self.rech_api.recharge(money, code)
        # 断言
        common_assert(resp, exp_d, exp_s)

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        self.login_api.user_login('18928450131', 'qqq123')
        # 获取充值图片验证码
        self.rech_api.get_recharge_verify_code(0.133333)
        # 充值
        resp = self.rech_api.recharge(100000, 8888)
        # 获取第三方开户接口的url
        data_list = html_util(resp)
        url = data_list[0]
        # 获取第三方开户接口的请求体数据
        form_data = data_list[1]
        # 获取第三方接口返回的响应结果
        resp1 = self.rech_api.third_recharge(url, form_data)
        # 断言
        assert 200 == resp1.status_code
        assert 'OK' in resp1.text
